using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace MultiLayer
{
    public class Screen
    {
        private Screen()
        {
            this.mEntities = new List<Entity>();
            this.mSelectedPen = new Pen(Color.Blue, 1.0f);
            this.mSelectedPen.DashStyle = DashStyle.Dash;
        }

        public static Screen Current
        {
            get
            {
                if (sCurrent == null)
                {
                    sCurrent = new Screen();
                }
                return sCurrent;
            }
        }

        public Entity SelectedEntity
        {
            get
            {
                return this.mSelectedEntity;
            }
        }

        public void AddEntity(Entity pEntity)
        {
            this.mEntities.Add(pEntity);
        }

        public void RemoveEntity(Entity pEntity)
        {
            this.mEntities.Remove(pEntity);
        }

        public Entity HitTest(int pX, int pY)
        {
            foreach (Entity entity in this.mEntities)
            {
                if (entity.HitTest(pX, pY))
                {
                    this.mSelectedEntity = entity;
                    return entity;
                }
            }
            return null;
        }

        public void Draw(Graphics pGraphics)
        {
            foreach (Entity entity in this.mEntities)
            {
                entity.Draw(pGraphics);
            }
            // now draw something around the selected entity
            if (this.mSelectedEntity != null)
            {
                Rectangle boundingBox = this.mSelectedEntity.BoundingBox;
                pGraphics.DrawRectangle(this.mSelectedPen, boundingBox);
                pGraphics.FillRectangle(Brushes.Red, new Rectangle(boundingBox.X - 50, boundingBox.Y - 50, 100, 100));
                pGraphics.FillRectangle(Brushes.Red, new Rectangle(boundingBox.X - 50, boundingBox.Y + boundingBox.Height - 50, 100, 100));
                pGraphics.FillRectangle(Brushes.Red, new Rectangle(boundingBox.X + boundingBox.Width - 50, boundingBox.Y - 50, 100, 100));
                pGraphics.FillRectangle(Brushes.Red, new Rectangle(boundingBox.X + boundingBox.Width - 50, boundingBox.Y + boundingBox.Height - 50, 100, 100));
            }
        }

        private static Screen sCurrent;
        private List<Entity> mEntities;
        private Entity mSelectedEntity;
        private Pen mSelectedPen;
    }
}
